const hre = require("hardhat");
const utils = require('./utils/utils');
const TokenUtils = require('./utils/pubToken');
const FactoryUtils = require('./utils/factoryWork');
const RouterUtils = require('./utils/routerWork');

/**
 * tf 期货部署
 */

async function main() {
  const [forLiquidity, forSwap] = await hre.ethers.getSigners();
  const amountA = utils.addZero(1, 18);
  const amountB = utils.addZero(45, 17);
  const timer = parseInt(Date.now() / 1000)
  const config = {
      startTime: 1642043872,
      endTime: 666666666666,
      amountA,
      amountB,
      // 流动性充的量
      amountADesired: utils.addZero(1, 12),
      amountBDesired: utils.addZero(4, 12),
      // addLiquidity swap两两比例相同
      // addLiquidity
      amountAMin: utils.addZero(1, 12),
      amountBMin: utils.addZero(4, 12),
      // swap
      amountOut: utils.addZero(1, 10),
      amountInMax: utils.addZero(4, 10),
      // removeLiquidity
      // liquidity: utils.addZero(1, 12),
      // amountAMin: 0,
      // amountBMin: 0
  }

  //发布2个币
  const { tokenA, tokenB } = await TokenUtils.pubToken(forLiquidity, forSwap, config)


  //部署工厂合约
  const factoryContract  = await FactoryUtils.factory(forLiquidity)


  //创建交易对
  const pairAddress = await FactoryUtils.createPair(factoryContract, tokenA, tokenB, config)

  //部署router合约
  const uniRouter = await RouterUtils.router(factoryContract.address)

  //token对router合约授权
  await TokenUtils.approve(uniRouter.address, tokenA, tokenB)
  //查询tokend余额
  const afterMint = await TokenUtils.checkBalance(forLiquidity, tokenA, tokenB, config)

  //添加池子
  const res = await RouterUtils.addLiquidity(forLiquidity, uniRouter, tokenA.address, tokenB.address, config)
  const afterAddLiquidity = await TokenUtils.checkBalance(forLiquidity, tokenA, tokenB, config)

}


main().catch((error) => {
  console.error(error);
  process.exitCode = 1;
});
